package com.leizi.igym.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.TransportOptions;
import co.elastic.clients.transport.Version;
import co.elastic.clients.transport.rest_client.RestClientOptions;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.AutoCloseableElasticsearchClient;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.util.Assert;

import java.util.function.Consumer;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-20  22:07
 * @Description:
 */

@Configuration
public class ESClientConfig extends ElasticsearchConfiguration {

    @Autowired
    ObjectMapper objectMapper;

    @Override
    public ClientConfiguration clientConfiguration() {
        return ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
    }

    // @Bean(name = "esClient")
    // public ElasticsearchClient elasticsearchClient(RestClient restClient) {
    //
    //     Assert.notNull(restClient, "restClient must not be null");
    //     TransportOptions.Builder transportOptionsBuilder = new RestClientOptions(RequestOptions.DEFAULT).toBuilder();
    //
    //     ContentType jsonContentType = Version.VERSION == null ? ContentType.APPLICATION_JSON
    //             : ContentType.create("application/vnd.elasticsearch+json",
    //             new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));
    //
    //     Consumer<String> setHeaderIfNotPresent = header -> {
    //         if (transportOptionsBuilder.build().headers().stream() //
    //                 .noneMatch((h) -> h.getKey().equalsIgnoreCase(header))) {
    //             // need to add the compatibility header, this is only done automatically when not passing in custom options.
    //             // code copied from RestClientTransport as it is not available outside the package
    //             transportOptionsBuilder.addHeader(header, jsonContentType.toString());
    //         }
    //     };
    //
    //     setHeaderIfNotPresent.accept("Content-Type");
    //     setHeaderIfNotPresent.accept("Accept");
    //     TransportOptions transportOptionsWithHeader = transportOptionsBuilder
    //             .addHeader("X_SPRING_DATA_ELASTICSEARCH_CLIENT", "reactive").build();
    //
    //     RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper(objectMapper), (RestClientOptions) transportOptionsWithHeader);
    //
    //     return new AutoCloseableElasticsearchClient(transport);
    // }


    @Override
    public JsonpMapper jsonpMapper() {
        return new JacksonJsonpMapper(objectMapper);
    }
}
